import scrapy
import os
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from webfin.items import stockItem, sentiItem
from datetime import date
import pymysql

class StockSpider(scrapy.Spider):
    name = 'stock'
    allowed_domains = ['*']
    start_urls = ['https://finance.ifeng.com/app/hq/stock/sh000001/']

    def __init__(self, tscode=None):
        super().__init__()
        self.goal = tscode
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('log-level=2')
        chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
        self.driver = webdriver.Chrome(chrome_options=chrome_options)

    def parse(self, response):
        item = stockItem()
        self.driver.implicitly_wait(4)  # 2为timeout,即等待的最长时间

        try:
            self.driver.get(response.url)

            textbox = self.driver.find_element_by_xpath("//form//input[@name='keyword']")
            button = self.driver.find_element_by_xpath("//form//input[@class='btn']")

            textbox.send_keys(self.goal)
            button.click()
            n = self.driver.window_handles
            self.driver.switch_to_window(n[-1])

            item['tscode'] = '\'' + str(self.goal) + '\''
            print(item['tscode'])
            item['tradedate'] = '\'' + str(date.today()) + '\''
            print(item['tradedate'])

            try:
                Sopen = self.driver.find_element_by_xpath("//div[@class='picForme']//tbody//tr//td//em[@id='open']")
                print(Sopen.text)
                item['open'] = Sopen.text

                Shigh = self.driver.find_element_by_xpath("//div[@class='picForme']//tbody//tr//td//em[@id='high']")
                print(Shigh.text)
                item['high'] = Shigh.text

                Sclose = self.driver.find_element_by_xpath("//div[@class='picForme']//tbody//tr//td//span[@id='hst_close']")
                print(Sclose.text)
                item['close'] = Sclose.text

                Slow = self.driver.find_element_by_xpath("//div[@class='picForme']//tbody//tr//td//em[@id='low']")
                print(Slow.text)
                item['low'] = Slow.text

                Svol = self.driver.find_element_by_xpath("//div[@class='picForme']//tbody//tr//td//span[@id='volume']")
                print(Svol.text[0:-1])
                item['vol'] = Svol.text[0:-1]

                Samount = self.driver.find_element_by_xpath("//div[@class='picForme']//tbody//tr//td//span[@id='amount']")
                print(Samount.text[0:-2])
                item['amount'] = Samount.text[0:-2]

            except:
                print("no such attr!")

            yield item

        except:
            pass
